/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool. 

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.
  
*/

#include "material.h"
#include <iostream>


CMaterial::CMaterial()
{
	// Default material:
	for (short i = 0; i < 4; i++)
	{
		color3ub[i] = 255;
		ambient[i] = 0.0;
		diffuse[i] = 0.0;
		specular[i] = 0.0;
	}
	shininess = 0.0;
	transparent = false;
	castShadows = false;
	receiveShadows = false;
	useShader = false;
	shaderName = "";
	gpuProgramID = 0;
}

CMaterial::CMaterial(const CMaterial &mat_in)
{
	for (short i = 0; i < 4; i++)
	{
		color3ub[i] = mat_in.color3ub[i];
		ambient[i] = mat_in.ambient[i];
		diffuse[i] = mat_in.diffuse[i];
		specular[i] = mat_in.specular[i];
	}
	shininess = mat_in.shininess;
	transparent = mat_in.transparent;
	castShadows = mat_in.castShadows;
	receiveShadows = mat_in.receiveShadows;
	gpuProgramID = mat_in.gpuProgramID;
	useShader = mat_in.useShader;
	shaderName = mat_in.shaderName;
}

CMaterial::~CMaterial()
{

}

CMaterial& CMaterial::operator= (CMaterial& mat_in)
{
	if (this == &mat_in)
		return *this;

	for (short i = 0; i < 4; i++)
	{
		color3ub[i] = mat_in.color3ub[i];

		ambient[i] = mat_in.ambient[i];
		diffuse[i] = mat_in.diffuse[i];
		specular[i] = mat_in.specular[i];
	}
	shininess = mat_in.shininess;
	shaderName = mat_in.shaderName;
	gpuProgramID = mat_in.gpuProgramID;

	return *this;
}

CMaterial& CMaterial::operator= (ModelOBJ::Material mat_in)
{
		for (short i = 0; i < 4; i++)
		{
			if (i < 3)
				color3ub[i] = 255;

			ambient[i] = mat_in.ambient[i];
			diffuse[i] = mat_in.diffuse[i];
			specular[i] = mat_in.specular[i];
		}
		shininess = mat_in.shininess;
		color3ub[3] = mat_in.alpha;

		return *this;
}

void CMaterial::SetColor(int red, int green, int blue, int alpha )
{
	color3ub[0] = red;
	color3ub[1] = green;
	color3ub[2] = blue;
	color3ub[3] = alpha;
}

void CMaterial::SetAmbient(float red, float green, float blue, float alpha)
{
	ambient[0] = red;
	ambient[1] = green;
	ambient[2] = blue;
	ambient[3] = alpha;
}

void CMaterial::SetDiffuse(float red, float green, float blue, float alpha)
{
	diffuse[0] = red;
	diffuse[1] = green;
	diffuse[2] = blue;
	diffuse[3] = alpha;
}

void CMaterial::SetSpecular(float red, float green, float blue, float alpha)
{
	specular[0] = red;
	specular[1] = green;
	specular[2] = blue;
	specular[3] = alpha;
}

void CMaterial::SetShininess(float shine)
{
	shininess = shine;
}

void CMaterial::SetGPUProgram(string shaderName)
{
	this->shaderName = shaderName;
}

void CMaterial::SetSurfaceTransparent(bool alphaon)
{
	transparent = alphaon;
}

void CMaterial::SetShadowBehaviour(bool cast, bool receive)
{
	castShadows = cast;
	receiveShadows = receive;
}

bool CMaterial::IsTransparent()
{
	return transparent;
}

bool CMaterial::SurfaceCastsShadows()
{
	return castShadows;
}

bool CMaterial::SurfaceReceivesShadows()
{
	return receiveShadows;
}

bool CMaterial::UseShader()
{
	return useShader;
}
